package oop.ex2.expression;

import java.util.ArrayList;

import oop.ex2.exceptions.UnrecognizedVariable;

/**
 * This node implements a binary operator that can be customized to use any function
 * node in order to calculate it's value.
 * It takes as input  a function node which is used for the calculations.
 * The supplied function node must accept two variables as input as verified by the function's
 * isBoolean method.
 * @author daniel_i_l
 *
 */
public class CustomBinaryNode extends BinaryOpNode {
	private FunctionNode _functionNode;
	
	/**
	 * Build a custom binary node using the operation string and the function node
	 * which should be used to calculate the node's value.
	 * @param matchedString The string that was matched in order to created this node.
	 * @param functionNode The function that should be used to calculate the value. It must
	 * accept two values as input.
	 */
	public CustomBinaryNode(String matchedString, FunctionNode functionNode) {
		super(matchedString);
		_functionNode = functionNode;
	}

	@Override
	public double calculateNode() throws UnrecognizedVariable {
		ArrayList<Double> arguments = new ArrayList<Double>();
		for (ExpressionNode node : _children)
			arguments.add(node.calculateNode());
		
		return _functionNode.calculateFunctionFromArgs(arguments);
	}

}
